package com.erp.erp_mask.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.erp_mask.mapper.TBomMapper;
import com.erp.erp_mask.mapper.TManufactureplanMapper;
import com.erp.erp_mask.mapper.TProcessMapper;
import com.erp.erp_mask.mapper.TProentrepotMapper;
import com.erp.erp_mask.pojo.TBom;
import com.erp.erp_mask.pojo.TProcess;
import com.erp.erp_mask.pojo.TProentrepot;
import com.erp.erp_mask.service.TBomService;
import com.erp.erp_mask.service.TProcessService;
import com.erp.erp_mask.vo.LingText;
import com.erp.erp_mask.vo.Result;
import com.erp.erp_mask.vo.StateData;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/pro")
public class TProcessController {

    @Resource
    private TProcessMapper tProcessMapper;
    @Resource
    private TBomMapper tBomMapper;
    @Resource
    private TProcessService tProcessService;
    @Resource
    private TProentrepotMapper tProentrepotMapper;

    @GetMapping("/processing")
    private ModelAndView index() {
        return new ModelAndView("pro/process");
    }

    @GetMapping("/searchpro")
    public Result indexpro(String opendate, String comeTime, Integer stateId) {
        return Result.success(tProcessService.listsearch(opendate, comeTime, stateId));
    }

    //领料
    @GetMapping("/lbomsearch")
    public Result lbomsearch(Integer level) {
        LambdaQueryWrapper<TBom> wrapper = Wrappers.lambdaQuery();
        if (level != null) {
            wrapper.eq(TBom::getBomLevel, level);
        }
        return Result.success(tBomMapper.selectList(wrapper));
    }

    @PutMapping("/lsearchintname")
    public Result lsearchintname(@RequestBody StateData stateData) {
        try {
            tProcessMapper.proedit(stateData);
            return Result.success("生产完成...!");
        } catch (Exception e) {
            return Result.fail("生产未完成...!");
        }
    }

    @GetMapping("/searchnum")
    public Result searchnum(String name) {
        return Result.success(tProcessMapper.searchintname(name));
    }

    @PutMapping("/ltpedit")
    public Result ltprocessedit(@RequestBody LingText lingText) {
        System.out.println("领料");
        try {
            tProcessMapper.ltpedit(lingText.getName(), lingText.getValue());
            return Result.success("领料成功");
        } catch (Exception e) {
            return Result.fail("领料失败!");
        }
    }

    @GetMapping("/lstateedit")
    public void lstateedit(TProcess tProcess) {
        tProcessMapper.lstateedit(tProcess);
    }

    @PutMapping("/ordedit")
    public void ordedit(@RequestBody StateData stateData) {
        System.out.println(stateData);
        tProcessMapper.ordedit(stateData);
    }

    @PutMapping("/numedit")
    public void numedit(@RequestBody StateData stateData) {
        System.out.println(stateData);
        tProcessMapper.numedit(stateData);
    }

    @PostMapping("/checkadd")
    public Result checkadd(@RequestBody TProcess tProcess) {
        try {
            tProentrepotMapper.insertdatalist(tProcess);
            return Result.success("已添加到生产仓库!");
        } catch (Exception e) {
            return Result.fail("未能成功添加到生产仓库!");
        }
    }
}
